package com.sky.order.service.impl;

import com.sky.order.dao.IOrderDao;
import com.sky.order.domain.Order;
import com.sky.order.domain.User;
import com.sky.order.domain.common.Response;
import com.sky.order.service.IOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Slf4j
@Service
public class OrderServiceImpl implements IOrderService {

    @Autowired
    private IOrderDao orderDao;

    @Autowired
    private RestTemplate restTemplate;

    @Override
    public Order queryOrderById(String orderId) {
        log.info("订单服务查询订单：{}", orderId);

        Order order = orderDao.queryOrderById(orderId);
        String url = "http://user-service/api/v1/user/query_user/" + order.getUserId();

        ParameterizedTypeReference<Response<User>> typeRef = new ParameterizedTypeReference<Response<User>>() {
        };

        ResponseEntity<Response<User>> exchange = restTemplate.exchange(url, HttpMethod.GET, null, typeRef);
        if (exchange.getBody().getCode().equals(Response.ResStatus.SUCCESS.getCode())) {
            order.setUser(exchange.getBody().getData());
        }
        return order;
    }
}
